
package com.idealighter.game.dictionary.dic;

import com.google.common.collect.ArrayListMultimap;
import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.idealighter.game.dictionary.core.DictionaryTable;
import com.idealighter.game.dictionary.domain.FruitslotLineCoordDomain;
import com.idealighter.game.dictionary.mapper.FruitslotLineCoordMapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Singleton
public class FruitslotLineCoordDic implements DictionaryTable<FruitslotLineCoordDomain> {

  private FruitslotLineCoordMapper mapper;
  private List<FruitslotLineCoordDomain> list;
  private Map<Integer, FruitslotLineCoordDomain> map;

  // 坐标集,key为行号,行号从1开始
  public ArrayListMultimap<Integer, Coordinate> coordinates;

  @Inject
  public FruitslotLineCoordDic(FruitslotLineCoordMapper mapper) {
    this.mapper = mapper;
    load();
  }

  @Override
  public List<FruitslotLineCoordDomain> list() {

    return list;
  }

  @Override
  public Map<Integer, FruitslotLineCoordDomain> map() {

    return map;
  }

  @Override
  public void load() {
    ArrayListMultimap<Integer, Coordinate> coordinates = ArrayListMultimap.create();

    this.list = mapper.selectAll();
    Map<Integer, FruitslotLineCoordDomain> map = new HashMap<>();
    list.forEach(val -> {
      map.put(val.getId(), val);
      coordinates.put(val.getLine(), new Coordinate(val.getRow(), val.getColumn()));
    });
    this.map = map;

    this.coordinates = coordinates;
  }

  @Override
  public FruitslotLineCoordDomain get(int id) {

    return map.get(id);
  }

  /**
   * 坐标.
   * 
   * @date 2015年9月29日 下午4:18:00
   *
   */
  public class Coordinate {
    // 行
    public final int row;
    // 列
    public final int column;

    public Coordinate(int row, int column) {
      this.row = row;
      this.column = column;
    }

  }
}
